package com.ksd.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.convert.CopyConvertUtils;
import com.ksd.modules.system.entity.AdminSystem;
import com.ksd.modules.system.enums.DefaultEnums;
import com.ksd.modules.system.mapper.AdminSystemMapper;
import com.ksd.modules.system.po.AdminSystemSavePO;
import com.ksd.modules.system.po.AdminSystemSearchPO;
import com.ksd.modules.system.service.IAdminSystemService;
import com.ksd.redis.util.RedisUtil_Back;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-16
 */
@Service
public class AdminSystemService extends BaseServiceImpl<AdminSystemMapper, AdminSystem> implements IAdminSystemService {

    @Autowired
    private RedisUtil_Back redisUtilBack;
    private static final String CACHE_KEY = "KSD:SYSTEM:MODEL:";

    @Override
    public long countSystem() {
        LambdaQueryWrapper<AdminSystem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSystem::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        return this.count(lambdaQueryWrapper);
    }

    @Override
    public Boolean selectSystemPassword(Long id, String password) {
        return true;
    }

    @Override
    public List<AdminSystem> findAdminSystemAll() {
        LambdaQueryWrapper<AdminSystem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSystem::getOpenFlag, DefaultEnums.YES.getCode());
        lambdaQueryWrapper.eq(AdminSystem::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.orderByDesc(AdminSystem::getCreateTime);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public Page<AdminSystem> findAdminSystem(AdminSystemSearchPO adminSystemSearchPO) {
        LambdaQueryWrapper<AdminSystem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(adminSystemSearchPO.getKeyword()), AdminSystem::getSysname, adminSystemSearchPO.getKeyword());
        lambdaQueryWrapper.eq(AdminSystem::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.orderByDesc(AdminSystem::getCreateTime);
        // 查询返回
        Page<AdminSystem> page = new Page<>(adminSystemSearchPO.getPageNo(), adminSystemSearchPO.getPageSize());
        return this.page(page, lambdaQueryWrapper);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUpdateAdminSystem(AdminSystemSavePO adminSystemSavePO) {
        AdminSystem adminSystem = CopyConvertUtils.copyProperties(adminSystemSavePO, AdminSystem.class);
        boolean b = this.saveOrUpdate(adminSystem);
        if(b) {
            redisUtilBack.del(CACHE_KEY + adminSystemSavePO.getId());
        }
        return b;
    }

    @Override
    public boolean deleteAdminSystemById(Long id) {
        AdminSystem adminSystem = new AdminSystem();
        adminSystem.setId(id);
        adminSystem.setIsDeleted(DefaultEnums.YES.getCode());
        boolean b =  this.updateById(adminSystem);
        if(b) {
            redisUtilBack.del(CACHE_KEY + id);
        }
        return b;
    }

    @Override
    public AdminSystem getAdminSystemById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询默认系统
     *
     * @return
     */
    @Override
    public AdminSystem getAdminSystemDefault() {
        LambdaQueryWrapper<AdminSystem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSystem::getDefaultFlag, DefaultEnums.YES.getCode());
        lambdaQueryWrapper.eq(AdminSystem::getOpenFlag, DefaultEnums.YES.getCode());
        AdminSystem one = this.getOne(lambdaQueryWrapper);
        if (Objects.isNull(one)) {
            LambdaQueryWrapper<AdminSystem> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(AdminSystem::getOpenFlag, DefaultEnums.YES.getCode());
            List<AdminSystem> list = this.list(lambdaQueryWrapper2);
            if (CollectionUtils.isNotEmpty(list)) {
                one = list.get(0);
            }
        }
        return one;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatusDefault(Long id) {
        // 1: 更新当前id其它的系统为非默认系统
        AdminSystem adminSystem = new AdminSystem();
        adminSystem.setDefaultFlag(0);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.ne("id", id);
        this.update(adminSystem, updateWrapper);
        // 2: 把当前改成1
        AdminSystem adminSystemd = new AdminSystem();
        adminSystemd.setDefaultFlag(1);
        adminSystemd.setId(id);
        boolean b =  this.updateById(adminSystem);
        if(b) {
            redisUtilBack.del(CACHE_KEY + id);
        }
        return b;
    }

    // 状态修改
    @Override
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b =  super.updateStatus(id,column,value);
        if(b) {
            redisUtilBack.del(CACHE_KEY + id);
        }
        return b;
    }

    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b =  super.updateStatusStr(id,column,value);
        if(b) {
            redisUtilBack.del(CACHE_KEY + id);
        }
        return b;
    }
}
